package com.huawei.app.demo.video;

import android.graphics.PixelFormat;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.huawei.app.demo.util.LogUtils;
import com.huawei.app.httpapi.IdeaHubHttpApiUtils;
import com.huawei.app.httpapi.ThreadPools;

/**
 * Surface窗口回调类
 *
 * @since 2021-01-06
 */
public class HDMISurfaceCallback implements SurfaceHolder.Callback {

    private static final String TAG = HDMISurfaceCallback.class.getSimpleName();
    private SurfaceView view;

    public HDMISurfaceCallback(SurfaceView view) {
        this.view = view;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        holder.setFormat(PixelFormat.TRANSPARENT);
        LogUtils.getInstance().i(TAG, "surfaceCreated");
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, final int width, final int height) {
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        final int left = location[0];
        final int top = location[1];

        String viewId = view.getResources().getResourceName(view.getId());
        LogUtils.getInstance().i(TAG, "surfaceChanged " + viewId
                + ", left=" + left + ", top=" + top
                + ", width=" + width + ", height=" + height);
        ThreadPools.run(new Runnable() {
            @Override
            public void run() {
                IdeaHubHttpApiUtils.getInstance().setCastWindowLayout(top, left, width, height);
            }
        });
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        LogUtils.getInstance().i(TAG, "surfaceDestroyed");
    }
}